#include "Storage.h"

Storage::Storage(void){
	testExamplesCount = 0;
	avgLength = 0;
	rolesCount = 0;
}

int Storage::save(char *filename){
	printf("Saving clusters ");
	filebuf f;
	if( f.open(filename, ios_base::out) != 0 ){
		ostream to(&f);
		to << testExamplesCount << "\n";
		to << avgLength << "\n";
		to << rolesCount << "\n";
		for( 
			StringIntMap::iterator role = rolesMap.begin();
			role != rolesMap.end();
			role++
		){
			to << role->first << ";" << role->second << ";";
		}
		to << "\n";

		to << clusters;
		f.close();
		printf("- finished!\n");
		return 0;
	}
	printf("- error!\n");
	return 1;
}

int Storage::load(char *filename){
	clusters.clear();
	rolesCount = 0;
	rolesMap.clear();

	printf("Loading clusters ");
	filebuf f;
	if( f.open(filename, ios_base::in) != 0 ){
		istream from(&f);
		char c;
		from >> testExamplesCount >> avgLength >> rolesCount;
		for( int i = 0; i < rolesCount; i++){
			from.get(c);
			string role = getLine(from, ';');
			long v;
			from >> v;
			rolesMap.insert(StringIntMap::value_type(role, v));
		}
		from.get(c);
		from.get(c);
		from >> clusters;
		f.close();
		printf("- finished!\n");
		return 0;
	}
	printf("- error!\n");
	return 1;
}

void Storage::toCSVFiles(char *learning, char *test, char *answers, int number){
	filebuf f;

	int count = 0, index;
	for( Clusters::iterator cluster = clusters.begin(); cluster != clusters.end(); cluster++){
		for( Sentences::iterator sentence = cluster->second.sentences.begin(); sentence != cluster->second.sentences.end(); sentence++){
			if( sentence->second.isWithFrame )
				continue;
			count++;
		}
	}

	index = 0;
	if( f.open(learning, ios_base::out) != 0 ){
		ostream out(&f);
		
		int k = 0;
		for( Clusters::iterator cluster = clusters.begin(); cluster != clusters.end(); cluster++){
			for( Sentences::iterator sentence = cluster->second.sentences.begin(); sentence != cluster->second.sentences.end(); sentence++){
				if( sentence->second.isWithFrame )
					continue;

				if( k == 0 ){
					for( unsigned int i = 0; i < sentence->second.features.size(); i++){
						if( i != 0 )
							out << ";";
						out << "Feature " << i;
					}
					out << ";Label\n";
					k++;
				}

				index++;
				if( number != 0 && index > count / 4 * (number - 1) && index <= count / 4 * number )
					continue;

				for( unsigned int i = 0; i < sentence->second.features.size(); i++){
					if( i != 0 )
						out << ";";
					out << sentence->second.features[i];
				}
				if( sentence->second.isFrameExample ){
					out << ";YES";
				}
				else{
					out << ";NO";
				}
				out << "\n";
			}
		}
		f.close();
	}

	index = 0;
	if( f.open(test, ios_base::out) != 0 ){
		ostream out(&f);

		int k = 0;
		for( Clusters::iterator cluster = clusters.begin(); cluster != clusters.end(); cluster++){
			for( Sentences::iterator sentence = cluster->second.sentences.begin(); sentence != cluster->second.sentences.end(); sentence++){
				if( sentence->second.isWithFrame )
					continue;
				if( k == 0 ){
					for( unsigned int i = 0; i < sentence->second.features.size(); i++){
						if( i != 0 )
							out << ";";
						out << "Feature " << i;
					}
					k++;
					out << "\n";
				}
				
				index++;
				if(  number != 0 && !(index > count / 4 * (number - 1) && index <= count / 4 * number) )
					continue;

				for( unsigned int i = 0; i < sentence->second.features.size(); i++){
					if( i != 0 )
						out << ";";
					out << sentence->second.features[i];
				}
				out << "\n";
			}
		}
		f.close();
	}

	index = 0;
	if( f.open(answers, ios_base::out) != 0 ){
		ostream out(&f);

		for( Clusters::iterator cluster = clusters.begin(); cluster != clusters.end(); cluster++){
			for( Sentences::iterator sentence = cluster->second.sentences.begin(); sentence != cluster->second.sentences.end(); sentence++){
				if( sentence->second.isWithFrame )
					continue;
				
				index++;
				if(  number != 0 && !(index > count / 4 * (number - 1) && index <= count / 4 * number) )
					continue;
				
				if( sentence->second.isFrameExample ){
					out << "YES";
				}
				else{
					out << "NO";
				}
				out << "\n";
			}
		}
	}
}